package com.geekbang.work9;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Configuration
@MapperScan(basePackages = "com.geekbang.work9.mapper",
        sqlSessionFactoryRef = "sqlSessionFactory")
public class DynamicDataSourceConfig {


    @Value("${spring.datasource.master.jdbc-url}")
    private String masterUrl;

    @Value("${spring.datasource.master.username}")
    private String masterUser;


    @Value("${spring.datasource.slave.jdbc-url}")
    private String slaveUrl;

    @Value("${spring.datasource.slave.username}")
    private String slaveUser;


    @Value("${spring.datasource.driver-class-name}")
    private String driverClass;

    @Autowired
    private MybatisProperties mybatisProperties;

    @Bean(name = "masterDataSource")
    @Primary
    public DataSource getMasterDateSource() {
        return getDataSource(masterUrl, masterUser, null);
    }


    private DataSource getDataSource(String url, String user, String pwd) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(pwd);
        return dataSource;
    }


    @Bean(name = "slaveDataSource")
    public DataSource getDateSourceV2() {
        return getDataSource(slaveUrl, slaveUser, null);
    }


    @Bean(name = "dynamicDataSource")
    public DynamicDataSource DataSource(@Qualifier("masterDataSource") DataSource masterDataSource, @Qualifier("slaveDataSource") DataSource slaveDataSource) {

        Map<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DataSourceType.DataBaseType.MASTER, masterDataSource);
        targetDataSource.put(DataSourceType.DataBaseType.SLAVE, slaveDataSource);
        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSource);
        dataSource.setDefaultTargetDataSource(masterDataSource);//设置默认对象
        return dataSource;
    }


    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory SqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dynamicDataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        bean.setConfiguration(mybatisProperties.getConfiguration());
        return bean.getObject();
    }

}
